<template>
  <div class="main-bg">
    <Nav></Nav>
    <div class="main-box">
      <div class="user-info">
        <div style="display:flex;">
          <img class="avatar" :src="userInfo.avatar" @click="changeAvatarVisible.value = true" />
          <div class="user-info-right">
            <div class="user-name">
              <div>{{ userInfo.userName }}</div>
              <div class="changeUsername" @click="changeNameVisible = true">修改</div>
            </div>
            <div class="email">{{ userInfo.email }}</div>
          </div>
        </div>
        <div>
          <el-button @click="(uploadArticlesVisible = true)">发布文章</el-button>
          <el-button @click="(uploadLogVisible = true)">上传日志</el-button>

        </div>
      </div>
      <div class="second-row">
        <div class="black-bg">
          <div class="title">提交领域分析</div>
          <div style="width:500px;height:400px;padding-top:10px;">
            <AreasChart :width="500" :height="400"></AreasChart>
          </div>
        </div>

        <div class="black-bg" style="width:680px;margin-left:20px;">
          <div class="title">
            <div>近期提交记录</div>
            <div class="del" @click="(isShowDel = !isShowDel)">删除日志</div>
          </div>
          <div class="log-data">
            <div class="log-data-item" v-for="(item, index) of logData" :key="index"
              @dblclick="toPage({ id: item.id }, item.type)"
              :style="{ backgroundColor: (index % 2 == 0) ? '#EEE' : '#fff' }">
              <div class="log-data-title">{{ item.title }}</div>
              <div style="font-size:14px;">{{ item.date }}
                <span class="del" v-if="isShowDel" @click="delLog(item.type, item.id)">删除</span>
              </div>

            </div>
          </div>
        </div>
      </div>
      <div class="upload-chart" style="flex-direction:column;margin-bottom: 30px;;">
        <div class="title">提交日历记录</div>
        <div style="align-self:center;">
          <UploadChart :width="900" :height="200"></UploadChart>
        </div>
      </div>
    </div>
  </div>
  <el-dialog v-model="changeNameVisible" title="修改昵称" width="30%">
    <el-input v-model="newNickName" placeholder="请输入新昵称"></el-input>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="submitChangeNickName">确定修改</el-button>
      </span>
    </template>
  </el-dialog>

  <el-dialog v-model="changeAvatarVisible.value" title="修改头像" width="30%">
    <UploadFile url="http://43.140.221.237:10001/userCenter/changeAvatar" :data="changeAvatarData" message="更改头像成功"
      :context="changeAvatarVisible"></UploadFile>
  </el-dialog>

  <el-dialog v-model="uploadLogVisible" title="上传日志" width="30%">
    <el-select v-model="uploadLogValue" class="m-2" placeholder="选择日志">
      <el-option v-for="item in uploadLogOptions" :key="item.value" :label="item.label" :value="item.value" />
    </el-select>
    <UploadFile url="http://43.140.221.237:10001/view/saveTrainLog" :data="changeAvatarData" message="上传日志成功"
      :context="changeUploadLogVisible" v-if="(uploadLogValue == 0)"></UploadFile>
    <UploadFile url="http://43.140.221.237:10001/view/saveLayerLog" :data="changeAvatarData" message="上传日志成功"
      :context="changeUploadLogVisible" v-if="(uploadLogValue == 1)"></UploadFile>
    <UploadFile url="http://43.140.221.237:10001/view/savePreLog" :data="changeAvatarData" message="上传日志成功"
      :context="changeUploadLogVisible" v-if="(uploadLogValue == 2)"></UploadFile>
  </el-dialog>
  <el-dialog v-model="uploadArticlesVisible" title="提交文章" width="50%">
    <uploadArticles></uploadArticles>
  </el-dialog>
</template>

<script setup>
import Nav from '@/components/Nav.vue'
import UploadChart from '@/components/chart/UploadChart.vue'
import AreasChart from '@/components/chart/AreasChart.vue'
import uploadArticles from '@/components/ArticleEditor.vue'
import { ElMessage } from "element-plus";
import UploadFile from '@/components/UploadFile.vue'
import { changeNickName, getAllLog, deleteTrainLog, deleteLayerLog, deletePreLog } from '@/api.js'
import { useRoute, useRouter } from "vue-router";
import { ref, getCurrentInstance } from "vue"
const router = useRouter();
const to = (url, query = {}) => {
  router.push({
    path: url,
    query
  })
}
const delLog = (type, id) => {
  if (type === 0) {
    deleteTrainLog(id).then(res => {
      console.log(res);
      if (res.SUCCESS) {
        ElMessage({
          message: "删除成功",
          type: "success",
        });
        location.reload();
      }
      else {
        ElMessage({
          message: "删除失败",
          type: "error",
        });
      }
    })
  }
  else if (type === 1) {
    deleteLayerLog(id).then(res => {
      console.log(res);
      if (res.SUCCESS) {
        ElMessage({
          message: "删除成功",
          type: "success",
        });
        location.reload();
      }
      else {
        ElMessage({
          message: "删除失败",
          type: "error",
        });
      }
    })
  }
  else if (type === 2) {
    deletePreLog(id).then(res => {
      console.log(res);
      if (res.SUCCESS) {
        ElMessage({
          message: "删除成功",
          type: "success",
        });
        location.reload();
      }
      else {
        ElMessage({
          message: "删除失败",
          type: "error",
        });
      }
    })
  }
}
const isShowDel = ref(false)
const toPage = (query, type) => {
  if (type == 0) {
    router.push({
      path: '/deviceInformation',
      query
    })
  }
  else if (type == 1) {
    router.push({
      path: '/visualization',
      query
    })
  }
  else if (type == 2) {
    router.push({
      path: '/preInformation',
      query
    })
  }

}
const uploadArticlesVisible = ref(false)
const uploadLogValue = ref(0);
const instance = getCurrentInstance();
const changeAvatarVisible = ref({ value: false })
const changeUploadLogVisible = ref({ value: false })
const changeNameVisible = ref(false)
const uploadLogVisible = ref(false)
const newNickName = ref('')
const userInfo = JSON.parse(
  localStorage.getItem('userInfo'));
console.log(userInfo);
const changeAvatarData = ref({
  uuid: userInfo.uuid
})
const uploadLogType = ref(0)
const submitChangeNickName = async () => {
  const uuid = userInfo.uuid
  console.log(uuid)
  const res = await changeNickName(uuid, newNickName.value)
  if (ressuccess) {
    ElMessage({
      message: "更改昵称成功",
      type: "success",
    });
    userInfo.userName = newNickName.value;
    localStorage.clear('userInfo')
    localStorage.setItem('userInfo', JSON.stringify(userInfo))
    instance.proxy.$forceUpdate();
    setTimeout(() => {
      changeNameVisible.value = false;
    }, 1000)
  }
  else {
    ElMessage({
      message: "更改昵称失败",
      type: "error",
    });
  }
}
const logData = ref([])
getAllLog(userInfo.uuid).then(res => {
  console.log(res);
  logData.value = res
})


const uploadLogOptions = [
  {
    value: 0,
    label: '训练日志',
  },
  {
    value: 1,
    label: '层日志',
  },
  {
    value: 2,
    label: '预处理日志',
  },
]
</script>

<style scoped>
.log-data-title {
  font-size: 16px;
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
  max-width: 400px;
}

.log-data-item {
  height: 45px;
  margin: 0 10px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 15px;

}

.log-data {
  height: 330px;
  overflow: auto;
  width: 92%;
  margin: 15px auto;

}

.log-data::-webkit-scrollbar {
  /*滚动条整体样式*/
  width: 10px;
  /*高宽分别对应横竖滚动条的尺寸*/
  height: 1px;
}

.log-data::-webkit-scrollbar-thumb {
  /*滚动条里面小方块*/
  border-radius: 10px;
  background-color: skyblue;
  background-image: -webkit-linear-gradient(45deg,
      rgba(255, 255, 255, 0.2) 25%,
      transparent 25%,
      transparent 50%,
      rgba(255, 255, 255, 0.2) 50%,
      rgba(255, 255, 255, 0.2) 75%,
      transparent 75%,
      transparent);
}

.log-data::-webkit-scrollbar-track {
  /*滚动条里面轨道*/
  box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
  background: #ededed;
  border-radius: 10px;
}

.main-bg {
  background-color: #f7f7f7;
  position: absolute;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.changeUsername {
  font-size: 12px;
  cursor: pointer;
}

.title {
  font-size: 20px;
  font-weight: 600;
  margin-top: 15px;
  margin-left: 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-right: 50px;
}

.user-info-right {
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  margin-left: 25px;
}

.black-bg {
  height: 400px;
  width: 500px;
  border-radius: 10px;
  background-color: #fff;
}

.avatar {
  width: 80px;
  height: 80px;
  border-radius: 10px;
  cursor: pointer;
}

.second-row {
  display: flex;
  width: 100%;
  margin-top: 35px;
}

.user-name {
  font-weight: 500;
  font-size: 18px;
}

.email {
  color: #6D757A;
  font-weight: 400px;
  font-size: 13px;
}

.user-info {
  width: 100%;
  display: flex;
  margin-top: 30px;
  justify-content: space-between;

}

.main-box {
  width: 1200px;
  display: flex;
  flex-direction: column;

}

.upload-chart {
  background-color: white;
  margin-top: 20px;
  display: flex;
  justify-content: center;
}

.del {
  color: rgba(65, 119, 247, 1);
  font-size: 12px;
}
</style>